package com.gzk.oeno.ihm.vin;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.gzk.oeno.business.vin.Vin;
import com.gzk.oeno.business.vin.VinManager;
import com.gzk.oeno.ihm.MainOenoFrame;
import com.gzk.oeno.ihm.tables.model.VinModel;
import com.gzk.oeno.ihm.vin.form.VinForm;
import com.gzk.utils.CustomException;
import com.gzk.utils.data.IdObject;
import com.gzk.utils.data.manager.event.CrudListener;
import com.gzk.utils.ihm.CustomSwingWorker;
import com.gzk.utils.ihm.UIUtils;
import com.gzk.utils.ihm.table.SmartTable;

public class AllVinPanel extends JPanel implements CrudListener{

	private class AddVinAction extends AbstractAction {

		private static final String NAME = "Ajouter un vin";
		
		public AddVinAction(){
			super(NAME, UIUtils.loadSilentIcon("icons/add_24.png"));
		}
		
		@Override
		public void actionPerformed(ActionEvent ev) {
			VinForm form = new VinForm();
			form.modifyOptions(true, false);
			form.showDialog(NAME);
		}
		
	}
	
	private class DoubleClicTableListener extends MouseAdapter {
	    
		public void mouseClicked(MouseEvent e) {
			if (e.getClickCount() > 1){
				Vin selection = getVinTable().getSelectedObject();
				
				MainOenoFrame.INSTANCE.addIFrame(new SingleVinIFrame(selection), true);
			}
		}
	}
	
	private class InitWorker extends CustomSwingWorker<Collection<Vin>, Void>{

		@Override
		protected void internalDone(Collection<Vin> result) throws CustomException {
			if (getVinTable() != null){
				List<Vin> vins = new ArrayList<>(result);
				getVinTable().updateData(vins);
			}
		}

		@Override
		protected Collection<Vin> doInBackground() throws Exception {
			return VinManager.getInstance().getAll();
		}
		
	}
	
	private JButton addButton;
	private SmartTable<Vin> table;
	
	public AllVinPanel(){
		super();
		VinManager.getInstance().addCrudListener(this);
		initGui();
		initData();
	}

	
	private void initData() {
		new InitWorker().execute();
	}


	private void initGui() {
		setLayout(new BorderLayout());
		
		add(new JScrollPane(getVinTable()), BorderLayout.CENTER);
		
		JPanel toolsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		toolsPanel.add(getAddButton());
		add(toolsPanel, BorderLayout.SOUTH);
	}


	protected JButton getAddButton() {
		if (addButton == null){
			addButton = new JButton(new AddVinAction());
		}
		return addButton;
	}

	protected SmartTable<Vin> getVinTable(){
		if (table == null){
			VinModel model = new VinModel();
			table = new SmartTable<>(model);
			table.addMouseListener(new DoubleClicTableListener());
		}
		return table;
	}

	@Override
	public void objectCruded(Object source, IdObject<?> object, CrudOperation operation) {
		initData();
	}
	
	
	
}
